//
//  BuyOneByManyCourseVC.m
//  HTP
//
//  Created by admin on 15/11/11.
//  Copyright © 2015年 nit. All rights reserved.
//

#import "BuyOneByManyCourseVC.h"
#import "MyFormViewController.h"
#import "PayOffOrderInfoViewController.h"

@interface BuyOneByManyCourseVC ()

@property (nonatomic,retain) UILabel *allCostLabel;
@property (nonatomic,retain) UILabel *timeLabel;
@end

@implementation BuyOneByManyCourseVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated {
    [self addCustomBackBase];
    [self addCustomTitleWithTitle:@"报名"];
}

- (void)initUI {
    [self.view setBackgroundColor:RGBCOLOR(240, 240, 240, 1)];
    if (self.view.subviews.count != 0) {
        return;
    }
    UIImageView *backImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenWidth / 2.0)];
    [backImage setBackgroundColor:[UIColor whiteColor]];
    [backImage setImage:[UIImage imageNamed:@"whitebg"]];
    [self.view addSubview:backImage];
    
    UILabel *nameLabel = [self getNormolLabel];
    [nameLabel setTextColor:RGBCOLOR(170, 170, 170, 1)];
    [nameLabel setFrame:CGRectMake(0, ScreenWidth / 4.0 - 45, ScreenWidth, 25)];
    nameLabel.text = [NSString stringWithFormat:@"%@的班课", _courseM.teacherName];
    [nameLabel setFont:[UIFont systemFontOfSize:18]];
    [self.view addSubview:nameLabel];
    
    UILabel *coursLabel = [self getNormolLabel];
    [coursLabel setFrame:CGRectMake(0, ScreenWidth / 4.0 , ScreenWidth, 25)];
    coursLabel.text = @"精品培优班";
    [coursLabel setFont:[UIFont systemFontOfSize:20]];
    [self.view addSubview:coursLabel];
    
    UILabel *addressLabel = [self getNormolLabel];
    [addressLabel setFrame:CGRectMake(0, ScreenWidth * 3 / 4.0 - 60, ScreenWidth, 25)];
    addressLabel.text = @"上课地址";
    [addressLabel setFont:[UIFont systemFontOfSize:19]];
    [self.view addSubview:addressLabel];
    
    UILabel *addressInfoLabel = [self getNormolLabel];
    [addressInfoLabel setFrame:CGRectMake(0, ScreenWidth * 3 / 4.0 - 24, ScreenWidth, 25)];
    addressInfoLabel.text = _courseM.tsAddress;
    [addressInfoLabel setTextColor:RGBCOLOR(170, 170, 170, 1)];
    [addressInfoLabel setFont:[UIFont systemFontOfSize:17]];
    [self.view addSubview:addressInfoLabel];
    
    UILabel *timePlanLabel = [self getNormolLabel];
    [timePlanLabel setFrame:CGRectMake(0, ScreenWidth * 5 / 4.0 - 120, ScreenWidth, 25)];
    timePlanLabel.text = @"时间安排";
    [timePlanLabel setFont:[UIFont systemFontOfSize:19]];
    [self.view addSubview:timePlanLabel];
    
    UILabel *startTimeLabel = [self getNormolLabel];
    [startTimeLabel setFrame:CGRectMake(0, ScreenWidth * 5 / 4.0 - 84, ScreenWidth, 25)];
    startTimeLabel.text = @"起始时间";
    [startTimeLabel setTextColor:RGBCOLOR(170, 170, 170, 1)];
    [startTimeLabel setFont:[UIFont systemFontOfSize:19]];
    [self.view addSubview:startTimeLabel];
    
    _timeLabel = [self getNormolLabel];
    [_timeLabel setFrame:CGRectMake(0, ScreenWidth * 5 / 4.0 - 44, ScreenWidth, 25)];
    _timeLabel.text = @"2015-10-11";
    _timeLabel.text = _courseM.tctStartDate;
    [self.view addSubview:_timeLabel];
    
    UIView *line1 = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenWidth * 5 / 6.0, ScreenWidth, 2)];
    [line1 setBackgroundColor:RGBCOLOR(184, 184, 184, 1)];
    [self.view addSubview:line1];
    
    UIView *line2 = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenWidth * 15 / 12.0, ScreenWidth, 2)];
    [line2 setBackgroundColor:RGBCOLOR(184, 184, 184, 1)];
    [self.view addSubview:line2];
    
    UIImageView *bottomImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, ScreenHeihgt - NavHe - 49, ScreenWidth, 49)];
    [bottomImage setImage:[UIImage imageNamed:@"pricebg"]];
    bottomImage.userInteractionEnabled = YES;
    [self.view addSubview:bottomImage];
    
    UIButton *contactBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 49 * 5.0 / 3.0, 49)];
    [contactBtn setBackgroundImage:[UIImage imageNamed:@"callme"] forState:UIControlStateNormal];
    [bottomImage addSubview:contactBtn];
    
    UIButton *payBtn = [[UIButton alloc] initWithFrame:CGRectMake(ScreenWidth - 80, 0, 80, 49)];
    [payBtn setTitle:@"立即支付" forState:UIControlStateNormal];
    [payBtn.titleLabel setFont:[UIFont systemFontOfSize:18]];
    [payBtn.titleLabel setTextAlignment:_TEXT_ALIGN_CENTER];
    [payBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [payBtn addTarget:self action:@selector(addOrder) forControlEvents:UIControlEventTouchUpInside];
    [bottomImage addSubview:payBtn];
    
    UIImageView *lineImage = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenWidth - 81, 8, 1, 33)];
    [lineImage setImage:[UIImage imageNamed:@"line_s"]];
    [bottomImage addSubview:lineImage];
    
    _allCostLabel = [self getNormolLabel];
    _allCostLabel.textAlignment = _TEXT_ALIGN_LEFT;
    _allCostLabel.font = [UIFont systemFontOfSize:18];
    NSMutableAttributedString *strCost = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"￥%.2lf",[_courseM.tsPrice floatValue]]];
    [strCost addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12] range:NSMakeRange(0, 1)];
    _allCostLabel.attributedText = strCost;
    [_allCostLabel setFrame:CGRectMake(CGRectGetMaxX(contactBtn.frame)+10, 0, ScreenWidth - 90 - CGRectGetMaxX(contactBtn.frame), 49)];
    [bottomImage addSubview:_allCostLabel];
    
}

//TODO:Action
/**
 *  添加订单
 */
-(void)addOrder{
    MyOrderViewModel *myOrderVM = [[MyOrderViewModel alloc] init];
    [myOrderVM setBlockWithReturnBlock:^(id returnValue) {
        [self dismissSVProgress];
        [self gotoOrderDetail:[NSString stringWithFormat:@"%@",returnValue[INTERFACE_INFO]]];
    } WithErrorBlock:^(id errorCode) {
        [self showSVProgressFailed:@"下单失败"];
    } WithFailureBlock:^{
        [self showSVProgressFailed:INTERFACE_FAILURE_CHIN];
    }];
    [self showSvprogress:@"生成订单"];
    [myOrderVM addOrderByParent:[USERCONTENT getUserParentId] withTeacherSetId:[NSString stringWithFormat:@"%@",_courseM.tsId] andConut:@"1"];
}

/**
 *  订单添加完成 转移至订单详情页面
 */
-(void)gotoOrderDetail:(NSString *)orderId{
    NSMutableArray *arrays = [NSMutableArray array];
    NSMutableArray *viewControllerArray = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
    [viewControllerArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (idx == 0) {
            [arrays addObject:obj];
        }
    }];
    [arrays addObject:[[MyFormViewController alloc] init]];
    PayOffOrderInfoViewController *orderInfo = [[PayOffOrderInfoViewController alloc] init];
    orderInfo.orderID = orderId;
    [arrays addObject:orderInfo];
    [self.navigationController setViewControllers:arrays animated:YES];
    //    OrderInfoViewController
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
